// const electron = require('electron');

const powerSaveBlocker = electron;

class TransferManager {
  constructor() {
    // 传输过程中不休眠
    this.blockPowerSaveOnTransimission = true;
    this.onInit();
  }

  setSDK(sdk) {
    this.sdk = sdk;
  }

  /**
   * 初始化
   */
  onInit() {
    // 关闭
    if (!this.blockPowerSaveOnTransimission) {
      this.tryStopPowerSaveBlocker();
      return;
    }
  }

  /**
   * 处理传输完毕的后续操作
   */
  handleAfterTransferFinished() {
    // 有传输任务时电脑不休眠
    // 传输结束要恢复，避免不必要的影响
    this.tryStopPowerSaveBlocker();
  }

  /**
   * 禁用休眠
   */
  tryStartPowerSaveBlocker() {
    if (
      this.powerSaveBlockerId &&
      powerSaveBlocker.isStarted(this.powerSaveBlockerId)
    ) {
      return;
    }
    this.powerSaveBlockerId = powerSaveBlocker.start('prevent-app-suspension');
  }

  /**
   * 解除禁用休眠
   */
  tryStopPowerSaveBlocker() {
    if (this.powerSaveBlockerId === undefined) return;
    if (powerSaveBlocker.isStarted(this.powerSaveBlockerId)) {
      powerSaveBlocker.stop(this.powerSaveBlockerId);
      this.powerSaveBlockerId = undefined;
    }
  }

  /**
   * @description 请求通知
   */
  requestNotification(options) {
    const notification = new electron.Notification(options);
    notification.show();
    return notification;
  }
}

module.exports = new TransferManager();
